home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte11 / heapaloc.c < prev    next >
C/C++ Source or Header  |  1996-01-29  |  7KB  |  214 lines

  1.  
  2. #include <windows.h>  
  3. #include "HeapAloc.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. #define BLOCKSIZE 32
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  26.                       LPTSTR lpCmdLine, int nCmdShow)
  27. {
  28.    MSG      msg;
  29.    HWND     hWnd; 
  30.    WNDCLASS wc;
  31.  
  32.    // Register the main application window class.
  33.    //............................................
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    // Create the main application window.
  56.    //....................................
  57.    hWnd = CreateWindow( lpszAppName, 
  58.                         lpszTitle,    
  59.                         WS_OVERLAPPEDWINDOW, 
  60.                         CW_USEDEFAULT, 0, 
  61.                         CW_USEDEFAULT, 0,  
  62.                         NULL,              
  63.                         NULL,              
  64.                         hInstance,         
  65.                         NULL               
  66.                       );
  67.  
  68.    if ( !hWnd ) 
  69.       return( FALSE );
  70.  
  71.    ShowWindow( hWnd, nCmdShow ); 
  72.    UpdateWindow( hWnd );         
  73.  
  74.    while( GetMessage( &msg, NULL, 0, 0) )   
  75.    {
  76.       TranslateMessage( &msg ); 
  77.       DispatchMessage( &msg );  
  78.    }
  79.  
  80.    return( msg.wParam ); 
  81. }
  82.  
  83.  
  84. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  85. {
  86.    WNDCLASSEX wcex;
  87.  
  88.    wcex.style         = lpwc->style;
  89.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  90.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  91.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  92.    wcex.hInstance     = lpwc->hInstance;
  93.    wcex.hIcon         = lpwc->hIcon;
  94.    wcex.hCursor       = lpwc->hCursor;
  95.    wcex.hbrBackground = lpwc->hbrBackground;
  96.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  97.    wcex.lpszClassName = lpwc->lpszClassName;
  98.  
  99.    // Added elements for Windows 95.
  100.    //...............................
  101.    wcex.cbSize = sizeof(WNDCLASSEX);
  102.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  103.                             IMAGE_ICON, 16, 16,
  104.                             LR_DEFAULTCOLOR );
  105.             
  106.    return RegisterClassEx( &wcex );
  107. }
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static LPTSTR* lpStrList;
  112. static int     nBlocks;
  113. static int     nStrings;
  114. static HANDLE  hHeap     = NULL;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE  : 
  119.               hHeap     = HeapCreate( 0, 1024, 0 );
  120.               lpStrList = HeapAlloc( hHeap, HEAP_ZERO_MEMORY, 
  121.                                      sizeof( LPTSTR )*BLOCKSIZE );
  122.               nBlocks   = 1;
  123.               nStrings  = 0;
  124.               break;
  125.  
  126.       case WM_COMMAND :
  127.               switch( LOWORD( wParam ) )
  128.               {
  129.                  case IDM_ALLOCATE :
  130.                         {
  131.                            // If there is no room in the array then allocate
  132.                            // more space to store the new item in.
  133.                            //.....................................
  134.                            if ( nStrings == nBlocks*BLOCKSIZE )
  135.                            {
  136.                               nBlocks++;
  137.                               lpStrList = HeapReAlloc( hHeap, HEAP_ZERO_MEMORY, 
  138.                                                        lpStrList, 
  139.                                                        sizeof( LPTSTR )
  140.                                                        *BLOCKSIZE*nBlocks );
  141.  
  142.                            }
  143.  
  144.                            *(lpStrList+nStrings) = HeapAlloc( hHeap,
  145.                                                       HEAP_ZERO_MEMORY, 12 );
  146.                            strcpy( *(lpStrList+nStrings), "Test String" );
  147.                            nStrings++;
  148.  
  149.                         }
  150.                         break;
  151.  
  152.                  case IDM_FREE :
  153.                         if ( nStrings > 0 )
  154.                         {
  155.                            nStrings--;
  156.                            HeapFree( hHeap, 0, *(lpStrList+nStrings) );
  157.  
  158.                            if ( nStrings < (nBlocks-1)*BLOCKSIZE )
  159.                            {
  160.                               nBlocks--;
  161.                               lpStrList = HeapReAlloc( hHeap, 0, 
  162.                                                        lpStrList,     
  163.                                                        sizeof( LPTSTR )
  164.                                                        *BLOCKSIZE*nBlocks );
  165.                            }
  166.                         }
  167.                         break;
  168.  
  169.                  case IDM_ABOUT :
  170.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  171.                         break;
  172.  
  173.                  case IDM_EXIT :
  174.                         DestroyWindow( hWnd );
  175.                         break;
  176.               }
  177.               break;
  178.       
  179.       case WM_DESTROY :
  180.               HeapDestroy( hHeap );
  181.               PostQuitMessage(0);
  182.               break;
  183.  
  184.       default :
  185.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  186.    }
  187.  
  188.    return( 0L );
  189. }
  190.  
  191.  
  192. LRESULT CALLBACK About( HWND hDlg,           
  193.                         UINT message,        
  194.                         WPARAM wParam,       
  195.                         LPARAM lParam)
  196. {
  197.    switch (message) 
  198.    {
  199.        case WM_INITDIALOG: 
  200.                return (TRUE);
  201.  
  202.        case WM_COMMAND:                              
  203.                if (   LOWORD(wParam) == IDOK         
  204.                    || LOWORD(wParam) == IDCANCEL)    
  205.                {
  206.                        EndDialog(hDlg, TRUE);        
  207.                        return (TRUE);
  208.                }
  209.                break;
  210.    }
  211.  
  212.    return (FALSE); 
  213. }
  214.